from typing import List
from llm.LLMProcessor import LLMProcessor
import re

class AnalyzeRelatedQuestion(LLMProcessor):
    """
    Analyze the core intent of this question and generate 3 alternative phrasings that:

    ###rules
    1.Maintain identical semantic meaning and required answer content.
    2.Use different sentence structures (interrogative forms, voice changes).
    3.Employ varied vocabulary while preserving technical terms.
    4.Include both concise and contextually expanded formulations.
    5.Cover potential keyword variations for retrieval systems.
    6.answer question in Chinese.
    7.Strictly follow the XML tag hierarchy.

    ###Original user question: {user_requirement}

    ###Format output as:
    <questions>
        <question>
            question 1
        </question>
        <question>
            question 2
        </question>
        <question>
            question 3
        </question>
    </questions>
    """

    def __init__(self, user_requirement: str):
        """Initialize with user requirement"""
        super().__init__()
        self.user_requirement = user_requirement
    
    def extract(self, content: str) -> tuple[bool, List[any]]:
        """Extract questions from XML formatted content"""
        # Find content between <questions> tags
        questions_match = re.search(r'<questions>(.*?)</questions>', content, re.DOTALL)
        if not questions_match:
            return False, []
        
        # Extract individual questions
        questions = re.findall(r'<question>(.*?)</question>', 
                             questions_match.group(1), 
                             re.DOTALL)
        
        # Clean up whitespace and return list
        return True, [q.strip() for q in questions]